package gui;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.JOptionPane;

import replication.RegServer_Replication;
import rmi.Server;

import messagepasser.UserInfo;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Registration.java
 *
 * Created on Mar 17, 2010, 8:32:54 PM
 */


/**
 *
 * @author keerthy
 */
public class Registration extends javax.swing.JFrame {
	JOptionPane popUp;
	Server s;
	UserInfo user = null;
	/** Creates new form Registration */
	public Registration() {
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jTextField2 = new javax.swing.JTextField();
		jButton1 = new javax.swing.JButton();
		jTextField3 = new javax.swing.JTextField();
		jPasswordField1 = new javax.swing.JPasswordField();
		jTextField1 = new javax.swing.JTextField();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setFont(new java.awt.Font("Arial", 1, 18));
		setForeground(new java.awt.Color(51, 0, 51));

		jTextField2.setColumns(10);
		jTextField2.setFont(new java.awt.Font("Verdana", 1, 14));

		jButton1.setFont(new java.awt.Font("Verdana", 1, 18));
		jButton1.setText("Register");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jTextField3.setColumns(10);
		jTextField3.setFont(new java.awt.Font("Verdana", 1, 14));
		jTextField3.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jTextField3ActionPerformed(evt);
			}
		});

		jPasswordField1.setColumns(10);
		jPasswordField1.setFont(new java.awt.Font("Verdana", 1, 14));

		jTextField1.setColumns(10);
		jTextField1.setFont(new java.awt.Font("Verdana", 1, 14));

		jLabel1.setFont(new java.awt.Font("Verdana", 1, 14));
		jLabel1.setText("First Name");

		jLabel2.setFont(new java.awt.Font("Verdana", 1, 14));
		jLabel2.setText("Last Name");

		jLabel3.setFont(new java.awt.Font("Verdana", 1, 14));
		jLabel3.setText("E-mail Address");

		jLabel4.setFont(new java.awt.Font("Verdana", 1, 14));
		jLabel4.setText("Password");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(layout.createSequentialGroup()
										.addGap(62, 62, 62)
										.addComponent(jButton1))
										.addGroup(layout.createSequentialGroup()
												.addGap(25, 25, 25)
												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel1)
														.addComponent(jLabel2)
														.addComponent(jLabel3)
														.addComponent(jLabel4))
														.addGap(42, 42, 42)
														.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
																.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
																.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
																.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
																.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
																.addContainerGap(37, Short.MAX_VALUE))
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addGap(21, 21, 21)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jLabel1))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(jLabel2))
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel3))
												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel4))
														.addGap(18, 18, 18)
														.addComponent(jButton1)
														.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);

		jTextField1.getAccessibleContext().setAccessibleName("firstName");

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed



	}//GEN-LAST:event_jTextField3ActionPerformed

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
		// TODO add your handling code here:


		String firstName;
		String lastName;
		String emailAddress;
		String password;
		
		RegServer_Replication repl = null;

		repl = new RegServer_Replication();
		s = repl.getServer();

		firstName=jTextField1.getText().trim();
		lastName=jTextField2.getText().trim();
		emailAddress=jTextField3.getText().trim();
		password=jPasswordField1.getText().trim();

		if (firstName == null || firstName.contains("@")) {
			popUp = new JOptionPane();
			popUp.showMessageDialog(null,
					"Enter FirstName ",
					"Inane error",
					JOptionPane.ERROR_MESSAGE);
			return;

		}if (lastName == null || lastName.contains("@")) {
			popUp = new JOptionPane();
			popUp.showMessageDialog(null,
					"Enter LastName ",
					"Inane error",
					JOptionPane.ERROR_MESSAGE);
			return;

		}
		else if (!emailAddress.contains("@") || emailAddress.contains("/")) {
			popUp = new JOptionPane();
			popUp.showMessageDialog(null,
					"Enter valid Email address",
					"Inane error",
					JOptionPane.ERROR_MESSAGE);
			jTextField3.setText(" ");

			return;

		} else if (emailAddress.startsWith("@") || emailAddress.endsWith("@")) {
			popUp = new JOptionPane();
			popUp.showMessageDialog(null,
					"Enter valid Email address",
					"Inane error",
					JOptionPane.ERROR_MESSAGE);
			jTextField3.setText(" ");
			return;
		}

		if (password == null || password.length() == 0) {
			popUp = new JOptionPane();
			popUp.showMessageDialog(null,
					"Enter Valid Password",
					"Inane error",
					JOptionPane.ERROR_MESSAGE);
			jPasswordField1.setText(" ");
			return;
		}
		Boolean userCreate = null;
		System.out.println("Sent Login Message After Registration");
		try {
			userCreate = s.createUser(emailAddress, firstName, lastName, password);
		} catch (Exception e2) {
			
			s = repl.getServer();
			try {
				userCreate = s.createUser(emailAddress, firstName, lastName, password);
			} catch (Exception e1) {
				
				repl.shutDown();
			}
		}
		if(userCreate){
			InetAddress addr = null;
			try { 
				addr = InetAddress.getLocalHost(); // Get IP Address 
			} catch (UnknownHostException e) { } 

			int port = 0;
			if (emailAddress.contains("rishit")) {
				port = 7777;
			}
			else {
				port = 6666;
			}

			try {

				try {
					user = s.login(emailAddress, password, addr.getHostAddress().toString(), port);
				}
				catch (Exception e) {
					s = repl.getServer();
					try {
						user = s.login(emailAddress, password, addr.getHostAddress().toString(), port);
					} catch (Exception e1) {
						repl.shutDown();
					}
				}

				if (user == null) {
					popUp.showMessageDialog(null,
							"Wrong user name or password :(",
							"Validation Error",
							JOptionPane.ERROR_MESSAGE);
					jPasswordField1.setText(" ");
					return;
				}
			} 
			catch (Exception e) {}

			System.out.println("Sent Login Message After Registration");
			this.setVisible(false);  
			new MainWindow(user,s, repl).setVisible(true);
		}
	}




	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton jButton1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JPasswordField jPasswordField1;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JTextField jTextField2;
	private javax.swing.JTextField jTextField3;
	// End of variables declaration//GEN-END:variables

}
